package com.todod.service;

import java.time.LocalDateTime;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.todod.common.util.SnowflakeIdGenerator;
import com.todod.mapper.PublicFileCollectMapper;
import com.todod.mapper.PublicFileMapper;
import com.todod.model.PublicFileCollect;

/**
 * @param
 * @author dyl
 * @date 2021-03-16
 */
@Service
@Transactional
public class PublicFileCollectService {
	@Autowired
	private PublicFileCollectMapper publicFileCollectMapper;
	@Autowired
	public PublicCatalogService publicCatalogService;
	@Autowired
	public PublicFileService publicFileService;
	@Autowired
	public PublicFileMapper publicFileMapper;

	public void saveData(Long _userId, Long fileId) {
		PublicFileCollect publicFileCollect = getAuthorityByFileId(fileId, _userId);
		if (publicFileCollect == null) {
			PublicFileCollect fileAuthorit = new PublicFileCollect();
			fileAuthorit.setId(SnowflakeIdGenerator.getId());
			fileAuthorit.setUserId(Long.valueOf(_userId));
			fileAuthorit.setFileId(fileId);
			fileAuthorit.setCreatetimeTi(LocalDateTime.now());
			fileAuthorit.setCreateuserId(_userId);

			publicFileCollectMapper.insert(fileAuthorit);
		}
	}

	public List<PublicFileCollect> getFileAuthorityByFileIdId(Long fileId) {
		QueryWrapper<PublicFileCollect> queryWrapper = new QueryWrapper<>();
		queryWrapper.eq("gfc_file_id", fileId);

		List<PublicFileCollect> list = publicFileCollectMapper.selectList(queryWrapper);
		return list;
	}

	public PublicFileCollect getAuthorityByFileId(String batchCode) {
		QueryWrapper<PublicFileCollect> queryWrapper = new QueryWrapper<>();
		queryWrapper.eq("gfc_file_id", batchCode);

		PublicFileCollect publicFileCollect = publicFileCollectMapper.selectOne(queryWrapper);
		return publicFileCollect;
	}

	public PublicFileCollect getAuthorityByFileId(Long fileId, Long userId) {
		QueryWrapper<PublicFileCollect> queryWrapper = new QueryWrapper<>();
		queryWrapper.eq("gfc_file_id", fileId);
		queryWrapper.eq("gfc_user_id", userId);

		PublicFileCollect publicFileCollect = publicFileCollectMapper.selectOne(queryWrapper);
		return publicFileCollect;
	}

	public List<PublicFileCollect> getFileAuthorityByUserId(Long userId) {
		QueryWrapper<PublicFileCollect> queryWrapper = new QueryWrapper<>();
		queryWrapper.eq("gfc_user_id", userId);

		List<PublicFileCollect> list = publicFileCollectMapper.selectList(queryWrapper);
		return list;
	}

}
